/*
 * Created by JFormDesigner on Sun Mar 23 17:08:36 CST 2025
 */

package com.xzp.bams.server;

import com.xzp.bams.service.Bank;
import com.xzp.bams.vo.RankVO;

import java.awt.*;
import java.awt.event.*;
import java.util.TreeSet;
import javax.swing.*;
import javax.swing.table.*;

/**
 * @author 许志鹏
 */
public class RankPanel extends JPanel {
    Bank bank =  Bank.getInstance();
    public RankPanel() {
        initComponents();
    }

    private void totalBalance(ActionEvent e) {
        double balance = bank.totalBalance();
        moneyLabel.setText(balance+"元");
        scrollPane.setViewportView(moneyLabel);
    }

    private void totalCeiling(ActionEvent e) {
        double celling = bank.totalCeiling();
        moneyLabel.setText(celling+"元");
        scrollPane.setViewportView(moneyLabel);
    }

    private void totalLoadAmount(ActionEvent e) {
        double load = bank.totalLoadAmount();
        moneyLabel.setText(load+"元");
        scrollPane.setViewportView(moneyLabel);

    }

    private void Rank(ActionEvent e) {
        TreeSet<RankVO> rank = bank.rank();

        //表格标题
        String[] titles = {"序号", "身份证号", "姓名", "总资产"};

        //表格数据
        Object[][] data = new Object[rank.size()][titles.length];
        //序号
        int index = 0;
        for (RankVO rankVO : rank) {
            data[index] = new Object[]{
                    ++index,
                    rankVO.getPersonId(),
                    rankVO.getName(),
                    rankVO.getTotal()
            };
        }
        DefaultTableModel tableModel = new DefaultTableModel(data, titles);
        JTable table = new JTable(tableModel);
        //将表格添加至scrollPane中
        scrollPane.setViewportView(table);
    }

    private void back(ActionEvent e) {
            CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
            cardLayout.show(this.getParent() , "serverPanel");

            //清空数据
            moneyLabel.setText("0.0元");
            moneyLabel.setHorizontalAlignment(SwingConstants.CENTER);
            scrollPane.setViewportView(moneyLabel);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        totalBalanceButton = new JButton();
        totalCeilingButton = new JButton();
        totalLoadAmountButton = new JButton();
        Rankbutton = new JButton();
        backButton = new JButton();
        scrollPane = new JScrollPane();
        moneyLabel = new JLabel();

        //======== this ========
        setLayout(null);

        //---- totalBalanceButton ----
        totalBalanceButton.setText("\u67e5\u770b\u603b\u4f59\u989d");
        totalBalanceButton.addActionListener(e -> totalBalance(e));
        add(totalBalanceButton);
        totalBalanceButton.setBounds(40, 40, 130, 30);

        //---- totalCeilingButton ----
        totalCeilingButton.setText("\u67e5\u770b\u900f\u652f\u603b\u989d");
        totalCeilingButton.addActionListener(e -> totalCeiling(e));
        add(totalCeilingButton);
        totalCeilingButton.setBounds(45, 110, 130, 30);

        //---- totalLoadAmountButton ----
        totalLoadAmountButton.setText("\u67e5\u770b\u8d37\u6b3e\u603b\u989d");
        totalLoadAmountButton.addActionListener(e -> totalLoadAmount(e));
        add(totalLoadAmountButton);
        totalLoadAmountButton.setBounds(45, 170, 125, 30);

        //---- Rankbutton ----
        Rankbutton.setText("\u67e5\u770b\u6392\u884c\u699c");
        Rankbutton.addActionListener(e -> Rank(e));
        add(Rankbutton);
        Rankbutton.setBounds(45, 235, 140, Rankbutton.getPreferredSize().height);

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(60, 305, 120, backButton.getPreferredSize().height);

        //======== scrollPane ========
        {

            //---- moneyLabel ----
            moneyLabel.setText("0.0");
            moneyLabel.setHorizontalAlignment(SwingConstants.CENTER);
            scrollPane.setViewportView(moneyLabel);
        }
        add(scrollPane);
        scrollPane.setBounds(225, 30, 300, 340);

        setPreferredSize(new Dimension(560, 450));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton totalBalanceButton;
    private JButton totalCeilingButton;
    private JButton totalLoadAmountButton;
    private JButton Rankbutton;
    private JButton backButton;
    private JScrollPane scrollPane;
    private JLabel moneyLabel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
